/**
 * Created by Administrator on 2017/1/18 0018.
 */
var pageSize = 2;//每页条数
var pageStart = 1;//当前页码
var pageCount = 10;//总页数
var islogin = 1;
$(function () {
    $(".fixed-bottom a").eq(0).click(function () {
        window.location.href = "card_weChat_student.html";
    });
    $(".fixed-bottom a").eq(1).click(function () {
        window.location.href = "apply.record_weChat_student.html";
    });
    $(".apply-btn-we").click(function () {
        window.location.href = "apply.detail_weChat_student.html";
    });
    init();
});

function init() {
    $.ajax({
        url: "/huaqiao/StudentFront/getCurStu?" + new Date().getTime(),
        async: false,
        success: function (str) {
            if (checkReturn(str)) {
                $("#stu_name").text(str.name);
                $("#class_name").text(str.classname);
            }
        }
    });
    getOutRequest();
}

function getOutRequest() {
    $.ajax({
        url: "/huaqiao/StudentFront/getOutRequest?" + new Date().getTime(),
        type: "post",
        data: {
            pageSize: pageSize,
            pageStart: pageStart
        },
        async: false,
        success: function (str) {
            if (checkReturn(str)) {
                $(".record>a").remove();
                for (var i = str.length-1; i >= 0 ; i--) {
                    var status_str = null;
                    var status_class = null;
                    switch (str[i].authStatus){
                        case 0 :
                            status_str = "审批中";
                            status_class = "state-ing";
                            break;
                        case 1 :
                            status_str = "审批通过";
                            status_class = "state-yes";
                            break;
                        case 2 :
                            status_str = "审批驳回";
                            status_class = "state-no";
                            break;
                        case 3 :
                            status_str = "撤销";
                            status_class = "state-cancel";
                            break;
                    }
                $(".record").prepend("<a href='#'><table class='apply-table-we text-center'><tr><td class='apply-td1'><p>"
                        +str[i].classname+"</p><p>"
                        +str[i].studentname+"</p></td><td class='apply-td2'><p>"
                        +DateUtils.format(new Date(str[i].startTime), 'yyyy-MM-dd HH:mm:ss')+"</p><p>至</p><p>"
                        +DateUtils.format(new Date(str[i].endTime), 'yyyy-MM-dd HH:mm:ss')+"</p></td><td class='apply-td3'><label>请假原因：</label><p class='apply-reason'>"
                        +str[i].requestReason.substr(0,2)+"</p> </td> <td class='apply-td4'> <p class="
                        +status_class+">"
                        +status_str+"</p></td></tr></table></a>")
                }
            }
        }
    });
    $.ajax({
        url: "/huaqiao/StudentFront/getOutRequest?" + new Date().getTime(),
        type: "post",
        async: false,
        success: function (str) {
            if (str != 0) {
                pageCount = Math.floor((str + pageSize - 1) / pageSize);
                setTableBTN();
            } else {
                layer.msg("没有数据", {time: 1000});
            }
        }
    });
}

function checkReturn(str) {
    if (str != null && str != "") {
        if (str.errorcode != undefined) {
            if (str.errorcode == 1) {
                if (islogin == 1) {
                    layer.msg(str.errormessage, {time: 1000});
                    // window.location.href = "login_pc_student.html";
                    islogin = 0;
                }
            }
            if (str.errorcode == 0) {
                layer.msg(str.errormessage, {time: 1000});
                return true;
            }
            return false;
        } else {
            return true;
        }
    } else {
        return false;
    }
}

function setTableBTN() {
    $("#page").unbind();

    $("#page option").remove();

    for(var i =1;i<=pageCount;i++){
        if(i!=pageStart)
            $("#page").append("<option>"+i+"</option>");
        else
            $("#page").append("<option selected=true>"+i+"</option>");
    }

    $("#page").change(function (i) {
        console.log($("#page").find("option:selected").text());
        pageStart = parseInt($("#page").find("option:selected").text());
        getOutRequest();
    });

    $(".page-nav span").each(function (i) {
        if (i == 1) {//上一页按钮
            this.onclick = function () {
                if (pageStart > 1) {
                    pageStart--;
                    getOutRequest();
                } else {
                    layer.msg("当前是第一页", {time: 1000});
                }
            };
        } else if (i == 2) {//下一页按钮
            this.onclick = function () {
                if (pageStart < pageCount) {
                    pageStart++;
                    getOutRequest();
                } else {
                    layer.msg("当前是最后一页", {time: 1000});
                }
            };
        } else if (i == 0) {
            this.onclick = function () {
                pageStart = 1;
                getOutRequest();
            };
        } else if (i == 3) {
            this.onclick = function () {
                pageStart = pageCount;
                getOutRequest();
            };
        }
    });
}